import json
import random

import allure
import requests


class Test_order():

    def random_id(self, l):

        r = random.randint(0, len(l) - 1)

        return l[r]

    # 3000-4000的概率为60%，4000-6000的概率为30%，6000以上的概率为10%
    def random_price(self):

        r = random.randint(0, 10)

        if r < 6:
            price = random.randint(3000, 4000)
        elif r >= 6 and r < 9:
            price = random.randint(4000, 6000)
        else:
            price = random.randint(6000, 10000)

        return price

    def random_amount(self):

        amount = random.randint(1, 100)

        return amount


    @allure.step("订单测试")
    def test_order(self):

        url="www.test.com/order/place"
        l=[1,5,7,11,12]
        id=self.random_id(l)
        price=self.random_price()
        amount=self.random_amount()

        param={
            "id":id,
            "price":price,
            "amount":amount
        }

        session=requests.Session()

        response=session.post(url=url,params=param)
        response.encoding="utf-8"

        code = response.status_code

        result=response.text
        result=json.loads(result)

        if code == 200 and result.get("code") == 100:
            print("测试成功")
        else:
            print("测试失败")

        assert code == 200 and result.get("code") == 100




